package io.github.annyylxyms.gynregistry;

import io.github.annyylxyms.gynregistry.cluster.Cluster;
import io.github.annyylxyms.gynregistry.health.GynHealthChecker;
import io.github.annyylxyms.gynregistry.health.HealthChecker;
import io.github.annyylxyms.gynregistry.service.GynRegistryService;
import io.github.annyylxyms.gynregistry.service.RegistryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * configuration for all beans.
 */
@Configuration
public class GynRegistryConfig {
    @Bean
    public RegistryService registryService() {
        return new GynRegistryService();
    }

    //registryService用Autowired保证和上面注入的RegistryService是同一个实例？
    //initMethod BEAN被注入的时候就执行start方法,注销时就执行stop方法
    @Bean(initMethod = "start", destroyMethod = "stop")
    public HealthChecker healthChecker(@Autowired RegistryService registryService) {
        return new GynHealthChecker(registryService);
    }

    @Bean(initMethod = "init")
    public Cluster cluster(@Autowired GynRegistryConfigProperties registryConfigProperties) {
        return new Cluster(registryConfigProperties);
    }

}
